import { createRouter, createWebHistory } from "vue-router"
import { ROUTERS_ENUMS } from '@/constant/system';

// 批量导入路由
const systemModules = import.meta.glob('./system/*.tsx', {eager: true});
const systemModulesRoutes = Object.values(systemModules).map((item: any) => item?.default);

const router = createRouter({
  // history: createWebHistory(import.meta.env.VITE_BASE_URL),
  history: createWebHistory(),
  routes: [
    {
      path: "/system",
      name: `${ROUTERS_ENUMS.SYSTEM}`,
      meta: { title: '系统' },
      component: () => import("@/layout/base/index.vue"),
      // component: DefaultLayout,
      children: [
        ...systemModulesRoutes.flat(),
      ],
      // redirect: { name: `${ROUTERS_ENUMS.HOME}Index` },
      redirect: { name: `${ROUTERS_ENUMS.SYSTEM}Indicator` },
    },
    {
      path: "/subapp",
      name: `SubApp`,
      meta: { title: '子应用' },
      component: () => import("@/layout/base/index.vue"),
      children: [
        {
          path: "demo1",
          name: "demo1",
          component: () => import("@/layout/wujie/index.vue"),
          meta: { 
            title: '子应用11111',
            activeMenu: '/subapp/demo1',
          },
        },
        {
          path: "demo2",
          name: "demo2",
          component: () => import("@/layout/wujie/index.vue"),
          meta: { 
            title: '子应用222222',
            activeMenu: '/subapp/demo2',
          },
        },
        {
          path: "demo3",
          name: "demo3",
          component: () => import("@/layout/wujie/index.vue"),
          meta: { 
            title: '子应用333333',
            activeMenu: '/subapp/demo3',
          },
        },
      ],
    },

    {
      path: "/",
      name: 'Home',
      // component: HomeLayout,
      meta: { title: '首页' },
      component: () => import("@/layout/home/index.vue"),
      // redirect: { name: `${ROUTERS_ENUMS.HOME}Index` },
      children: [
        {
          path: "",
          name: "HomeIndex",
          // component: Home,
          component: () => import("@/views/home/index.vue"),
        },
        {
          path: "/:id",
          name: "HomeDetail",
          // component: Detail,
          component: () => import("@/views/home/detail.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "LOGIN",
      meta: { title: '登录' },
      // component: Login,
      component: () => import("@/views/login/index.vue"),
    },
    {
      path: "/nearbyBranch",
      name: "NEARBY_BRANCH",
      meta: { title: '附近站点' },
      // component: NearbyBranch,
      component: () => import("@/views/nearby/index.vue"),
    },
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      meta: { title: '404' },
      component: () => import('@/views/404/index.vue'),
    },
  ],
})

router.afterEach((to) => {
  document.title = (to?.meta?.title || '自知无知') as any;
})

export default router
